package com.dabai.qrtools.ui;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dabai.qrtools.R;
import com.dabai.qrtools.adapter.SmallProgramAdapter;
import com.dabai.qrtools.bean.SmallProgramInfo;
import com.dabai.qrtools.data.InitSmallProgram;
import com.dabai.qrtools.other.MainUtils;
import com.dabai.qrtools.utils.ActivityUtils;
import com.dabai.qrtools.utils.DabaiUtils;
import com.dabai.qrtools.utils.ThemeUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.zxing.activity.CaptureActivity;
import com.google.zxing.util.Constant;
import com.permissionx.guolindev.PermissionX;

import java.util.ArrayList;
import java.util.Objects;

public class MoreActivity extends AppCompatActivity {

    private Context context;
    private MaterialToolbar mToolbar;
    private RecyclerView mRecyclerview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        initViews();


        context = this;

        ThemeUtils.setAutoUiModeStatusBar(this, this);
        setSupportActionBar(mToolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        initSmallProgram();
        initListenerEvent();

    }


    private void initListenerEvent() {
    }


    /**
     * 初始化小程序
     */
    @SuppressLint("WrongConstant")
    private void initSmallProgram() {


        mRecyclerview.setLayoutManager(new GridLayoutManager(this, 2));

        //准备数据
        ArrayList<SmallProgramInfo> smallProgramInflows = new ArrayList<>();

        InitSmallProgram.initSmallProgram(smallProgramInflows);

        //准备适配器
        SmallProgramAdapter smallProgramAdapter = new SmallProgramAdapter(smallProgramInflows);
        //设置适配器
        mRecyclerview.setAdapter(smallProgramAdapter);

        smallProgramAdapter.setOnItemClickListener((adapter, view, position) -> {

            int pos = position + 1;


            if (pos == 2) {

                PermissionX.init(this)
                        .permissions(Manifest.permission.CAMERA)
                        .request((allGranted, grantedList, deniedList) -> {
                            if (!allGranted) {
                                DabaiUtils.showToast(context, "需要拍照权限");
                            } else {
                                ActivityUtils.startActivityForResult(this, CaptureActivity.class, 101);
                            }
                        });


                return;
            }

            if (pos == 5 || pos == 6) {
                DabaiUtils.openLinkWithBrowserActivity(context, "https://support.qq.com/embed/phone/134211/blog/514830");
                return;
            }

            if (pos == 3 || pos ==8 ) {
                DabaiUtils.showDialog(context, "正在开发中...").getWindow().setWindowAnimations(R.style.DBAnim_Transitions_IOS);
                return;
            }

            if (pos == 7) {

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
                intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
                intent.setFlags(335544320);
                intent.setAction("android.intent.action.VIEW");

                MainUtils.addShortCut(context,"微信扫一扫",R.mipmap.ic_scan_qr,intent);

                return;
            }

            try {
                ActivityUtils.startActivity(context, ((SmallProgramInfo) adapter.getData().get(position)).getTagClass());
            } catch (Exception e) {
                e.printStackTrace();
            }

        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 101) {
                if (data != null) {

                    String content = data.getStringExtra(Constant.INTENT_EXTRA_KEY_QR_SCAN);
                    MainUtils.toScanQr(context,content);

                }
            }
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void initViews() {
        mToolbar = findViewById(R.id.toolBar);
        mRecyclerview = findViewById(R.id.recyclerView);
    }
}